<?php
/**
 * Created by PhpStorm.
 * User: DELL-PC
 * Date: 2018/9/4
 * Time: 11:45
 */

namespace Application\Common\Controller;


use J\Schema\Syntax;
use J\Exception;
use J\Intent;

class ManyElementUIActor extends ElementUIActor
{
    /**
     * 关联动作生成器
     *
     * @inheritdoc
     */
    protected function invokeIntentMethod(Intent $intent)
    {
        if ($this->controller->getInformation($intent, 'many', false) !== false) {
            if (__OPTION__ === 'Api') {
                $this->optionApiMany($intent);
            } elseif (__OPTION__ === 'ElementUI') {
                $this->optionElementUIMany($intent);
            } elseif (__OPTION__ === 'Data') {
                $this->optionDataMany($intent);
            } else {
                return false;
            }

            return null;
        }

        if ($this->controller->getInformation($intent, 'dnib', false) !== false) {
            if (__OPTION__ === 'Api') {
                $this->optionApiDnib($intent);
            } elseif (__OPTION__ === 'ElementUI') {
                $this->optionElementUIMany($intent);
            } elseif (__OPTION__ === 'Data') {
                $this->optionDataDnib($intent);
            } else {
                return false;
            }

            return null;
        }

        return parent::invokeIntentMethod($intent);
    }

    /**
     * 关联界面
     *
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionElementUIMany(Intent $intent)
    {
        $config = $this->controller->getInformation($intent);
        $he = array_isset($config,'many', array_isset($config, 'dnib'));

        $hisCollection = $this->controller->getCollection($he);

        $him = $hisCollection
            ->getCurd()->setWhere(Syntax::KEY_ID)->setElements(Syntax::KEY_ALL)->find();

        if(empty($him)) return;

        $r = ElementUI::getView($this->controller, $intent, $config, $this->controller->getCollection());

        if (!isset($r['view']['data'])) {
            $r['view']['data'] = $intent->makeUrl(null, '/data.php');
            $r['view']['data'] .= "/{$hisCollection->getName()}/" . $him[Syntax::KEY_ID];
        }

        $func = array_isset($config,'func', 'defaultTitle');
        if (is_callable([$this->controller, $func])) $r['title'] = $this->controller->{$func}($hisCollection, $him);
        $intent->value = $r;
    }

    /**
     * 获取关联信息 [he => he]
     *
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionDataMany(Intent $intent)
    {
        $config = $this->controller->getInformation($intent);
        $he = array_isset($config,'many');

        $r = $this->getData($config);
        $curd = new Many($this->getModel());
        // $raw = array_isset($config, Syntax::KEY_TREE, false);
        $r['selected'] = $curd->getBinds($he, null, true);
        $intent->value = $r;
    }

    /**
     * 重建关联信息  [he => he, me => ids]
     *
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiMany(Intent $intent)
    {
        $curd = new Many($this->getModel());
        $curd->setBindsElementUI($this->controller->getInformation($intent ,'many'));
        $this->next($intent, '修改成功');
    }

    /**
     * 单一解绑 [he => he,  me => id]
     *
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionApiDnib(Intent $intent)
    {
        $curd = new Many($this->getModel());
        $curd->dinbElementUI($this->controller->getInformation($intent, 'dnib'));
        $this->next($intent, '修改成功');
    }

    /**
     * 获取关联信息 [he => he]
     *
     * @param Intent $intent
     * @throws Exception
     */
    protected function optionDataDnib(Intent $intent)
    {
        $config = $this->controller->getInformation($intent);
        $he = array_isset($config,'dnib');
        $config['size'] = -1;
        $intent->value = $this->getData($config, [$he]);
    }
}